package com.yuanzong.wuliu.common.utils.log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;

public class MyLogUtils {

    public static void appendLog(String genPath, String content) {
        FileWriter fileWriter = null;
        try {
            // 构建文件夹
            new File(genPath).mkdirs();
            // 获取日期
            String dateStr = LocalDate.now().toString();
            String filePath = genPath + "/" + dateStr + ".txt";
            File file = new File(filePath);
            // 文件不存在则创建
            if (!file.exists()) {
                file.createNewFile();
            }
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            fileWriter = new FileWriter(filePath, true);
            fileWriter.write(content + "\n");
        } catch (Exception e) {
            System.out.println("保存自定义日志失败," + e.getMessage());
            e.printStackTrace();
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        appendLog("D:/log", "我是网吧选手");
    }

}
